package com.ruoyi.framework.config;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.JsonUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.common.utils.token.TokenUtil;
import com.ruoyi.common.utils.token.UserThreadLocal;
import com.ruoyi.framework.annotation.AllowAccess;
import com.ruoyi.framework.constant.HttpStatusEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;


public class LoginInterceptor implements HandlerInterceptor {

    private static Logger logger = LoggerFactory.getLogger(LoginInterceptor.class);


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        TokenUtil tokenUtil = SpringUtils.getBean(TokenUtil.class);

//        logger.info("LoginInterceptor preHandle==========进入拦截器");
        Map<String, Object> dataMap = new HashMap<>();

        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            Method method = handlerMethod.getMethod();

            // 判断接口是否需要登录
            AllowAccess methodAnnotation = method.getAnnotation(AllowAccess.class);
            if (methodAnnotation == null) {
                //获取请求头中的token
                String token = request.getHeader("token");
                if (StringUtils.isEmpty(token)) {
                    R ajaxResult = R.fail(HttpStatusEnum.TOKENNULL.getResultCode(), HttpStatusEnum.TOKENNULL.getResultMessage());
                    ServletUtils.renderString(response, JsonUtils.toJsonString(ajaxResult));
                    return false;
                }
                //判断token中是否存在用户信息
                Map<String, Object> userMap = tokenUtil.checkToken(token);
                if (userMap != null) {
                    dataMap.put("userMap", userMap);
                    UserThreadLocal.set(userMap);
                } else {
                    R ajaxResult = R.fail(HttpStatusEnum.TOKENERROR.getResultCode(), HttpStatusEnum.TOKENERROR.getResultMessage());
                    ServletUtils.renderString(response, JsonUtils.toJsonString(ajaxResult));
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }

}
